/**
 * -----------------------------------
 * Copyright (c) 2021-2023
 * All rights reserved, Designed By www.linfeng.tech
 * 林风社交论坛商业版本请务必保留此注释头信息
 * 商业版授权联系技术客服	 QQ:  3582996245
 * 严禁分享、盗用、转卖源码或非法牟利！
 * 版权所有 ，侵权必究！
 * -----------------------------------
 */
package io.linfeng.modules.app.controller;

import io.linfeng.common.utils.Result;
import io.linfeng.common.validator.ValidatorUtils;
import io.linfeng.common.vo.DiscussDetailResponse;
import io.linfeng.common.utils.AppPageUtils;
import io.linfeng.common.utils.R;
import io.linfeng.modules.admin.entity.AppUserEntity;
import io.linfeng.modules.admin.entity.DiscussEntity;
import io.linfeng.modules.admin.service.DiscussService;
import io.linfeng.modules.app.annotation.Login;
import io.linfeng.modules.app.annotation.LoginUser;
import io.linfeng.modules.app.param.DiscussAddForm;
import io.linfeng.modules.app.param.DiscussDeleteForm;
import io.linfeng.modules.app.param.DiscussListForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;


@RestController
@RequestMapping("app/discuss")
@Api(tags = "移动端——话题")
public class AppDiscussController {

    @Autowired
    private DiscussService discussService;

    /**
     * 话题列表
     */
    @PostMapping("/list")
    @ApiOperation("话题列表")
    public Result<AppPageUtils> list(@RequestBody DiscussListForm request){
        AppPageUtils page = discussService.getDiscussList(request);

        return new Result<AppPageUtils>().ok(page);
    }

    /**
     * 我创建的话题
     */
    @Login
    @PostMapping("/myDis")
    @ApiOperation("我创建的话题")
    public Result<AppPageUtils> myDiscuss(@RequestBody DiscussListForm request, @ApiIgnore @LoginUser AppUserEntity user){
        AppPageUtils page = discussService.myDiscuss(request,user);

        return new Result<AppPageUtils>().ok(page);
    }

    @Login
    @PostMapping("/del")
    @ApiOperation("圈主删除话题")
    public Result deleteDiscuss(@RequestBody DiscussDeleteForm request, @ApiIgnore @LoginUser AppUserEntity user){
        discussService.deleteDiscuss(request,user);
        return new Result();
    }

    @Login
    @PostMapping("/addDis")
    @ApiOperation("圈主创建话题")
    public Result addDiscuss(@RequestBody DiscussAddForm request, @ApiIgnore @LoginUser AppUserEntity user){
        ValidatorUtils.validateEntity(request);
        Boolean success=discussService.addDiscuss(request,user);
        if(success){
            return new Result().ok("话题创建完成");
        }
        return new Result().error("话题创建失败");
    }

    @GetMapping("/detail")
    @ApiOperation("话题详情页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "话题id",paramType = "query", dataType = "Integer", required = true)
    })
    public Result<DiscussDetailResponse> detail(@RequestParam Integer id){
        DiscussDetailResponse response=discussService.detail(id);

        return new Result<DiscussDetailResponse>().ok(response);
    }


    @Login
    @GetMapping("/checkJoinTopic")
    @ApiOperation("检查用户是否加入圈子")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "话题id",paramType = "query", dataType = "Integer", required = true)
    })
    public Result checkJoinTopic(@RequestParam Integer id, @ApiIgnore @LoginUser AppUserEntity user){
        discussService.checkJoinTopic(id,user.getUid());

        return new Result<>().ok();
    }


    @GetMapping("/discussList")
    @ApiOperation("热门话题-近一个月-取7条")
    public Result<List<DiscussEntity>> discussList(){
        List<DiscussEntity> list=discussService.discussList();

        return new Result<List<DiscussEntity>>().ok(list);
    }


    @GetMapping("/hotDiscussList")
    @ApiOperation("热门话题-平台综合-取20条")
    public Result<List<DiscussEntity>> hotDiscussList(){
        List<DiscussEntity> list=discussService.hotDiscussList();

        return new Result<List<DiscussEntity>>().ok(list);
    }

}
